package com.scalar.chiptrack.adaptor.msilica;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.utils.ParserResult;
import com.scalar.chiptrack.adaptor.utils.Parser;
import com.scalar.chiptrack.adaptor.utils.ParserFactory;
import com.scalar.chiptrack.adaptor.utils.XLSParserImpl;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 17 Sep, 2009
 * Time: 4:46:52 PM
 * To change this template use File | Settings | File Templates.
 */
public class SigurdDataParser extends JobHandler implements AdaptorConstants
{
    private HashMap<String, TestLot> m_testDataMap              = new HashMap<String, TestLot>();

    private Logger m_parserLogger       = null;
    private String m_dataOutputDir      = null;

    public  SigurdDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_SIGURD , WIP_TYPE_TEST );

            if ( m_dataOutputDir != null )
            {
                AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );

                m_parserLogger= AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_SIGURD );

                AdaptorUtils.setLogger( m_parserLogger );
                TestAdaptorDAO.setLogger( m_parserLogger);
            }
        }
        catch ( Exception se )
        {
            log( "ERROR!! ERROR!! ERROR!! Exception while loading Sigurd output directory details from database, Exception: " + se );
            System.out.println( "ERROR!! ERROR!! ERROR!! Exception while loading Sigurd output directory details from database, Exception: " + se );
        }
    }
    public static void main( String args[] ) throws Exception
    {
        try
        {
            new SigurdDataParser().execute();
        }
        catch ( Exception e )
        {
            throw e;
        }
    }
    public void execute() throws Exception
    {
        AdaptorUtils.printHeader( VENDOR_NAME_SIGURD, CONSTANT_PARSER );

        File doneDestDir = new File( m_dataOutputDir + "/done" );
        File dataoutDir  = new File( m_dataOutputDir );

        File[] fileList = dataoutDir.listFiles();

        if ( ( fileList == null ) || (fileList.length <= 3))
        {
            log("No file found in directory: " + dataoutDir);
            System.out.println("No file found in directory: " + dataoutDir );
            throw new ScalarException("No file found in directory: " + dataoutDir+ "----\n\n" );
        }

        ArrayList<File> successFileList = new ArrayList<File>();

        boolean success;

        for (File aFileList : fileList)
        {
            if (aFileList.isDirectory())
            {
                continue;
            }

            try
            {
                System.out.println("aFileList.getName() = " + aFileList.getName());
                if (aFileList.getName().startsWith(SigurdPOP3Adaptor.FILE_NAME_FT_WIP) || aFileList.getName().indexOf(SigurdPOP3Adaptor.FILE_NAME_FT_WIP) != -1)
                {
                    parseTestWipReport(aFileList.getAbsolutePath());

                    successFileList.add(aFileList);
                }

                success = true;
            }
            catch (Exception e)
            {
                success = false;
                log("Exception while parsing file: " + aFileList.getAbsolutePath() + "\nException: " + e);
                e.printStackTrace();
                throw e;
            }
        } //End of for Each

		try
        {
           // Saving FINAL TEST Information
           if ( m_testDataMap.isEmpty() == false )
            {
                log( " Updating Test Lot Information" );
                TestAdaptorDAO.saveTestLot( null,m_testDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_SIGURD );
            }
            else
           {
               TestAdaptorDAO.closeTestLot( null, LOCATION_TYPE_FT, VENDOR_NAME_SIGURD );
           }

           success = true;
        }
        catch ( ScalarException se )
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update FT Lot information into database: " + se );
            throw se;
        }

        // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_SIGURD,true, true, true );

        // To Close the Drop Out Lots
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_SIGURD );

        int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( SigurdPOP3Adaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);
        // transaction 9
        try
        {
            if (success)
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, SigurdPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println("SUCCESSFULLY UPDATED IN THE DATABASE");
            }
        }
        catch (ScalarException se)
        {
            success = false;  //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, SigurdPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log("WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se);
            throw se;
        }

        // Step 10
        // Move the successful files to 'done' directory
        if (success)
        {
            AdaptorUtils.moveFilesToDone( successFileList, doneDestDir);
        }
    }

    private void parseTestWipReport (String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result            = null;

        TestLot      testLot       = null;
        String partNumber       = null;
        String fabLotNumber     = null;
        String vendorLotNumber  = null;
        String dateIn           = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( !detailWipFile.exists() )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            Parser parser = ParserFactory.getXLSParser( detailWipFile );

            ((XLSParserImpl)parser).setHeaderRowIdx(3);
            ((XLSParserImpl)parser).setDataStartRowIdx(4);

            log( "\nFound " + fileName );
            log( "\nParsing the file for LFT Wip Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for LFT Wip Report..." );

            result = parser.parse();

            while ( result.next() )
            {
                partNumber      = result.getString("ProductNo");
                fabLotNumber    = result.getString("CustomerLotNo");
                vendorLotNumber = result.getString("LotNo");
                if ( vendorLotNumber == null )
                {
                    vendorLotNumber = fabLotNumber;
                }
                if ( partNumber != null && partNumber.trim().length() > 0 && fabLotNumber != null && fabLotNumber.trim().length() > 0 )
                {
                    if ( result.getDate( "WipDate") != null )
                    {
                        dateIn = StringUtils.formatDateToString( result.getDate( "WipDate"), "MM/dd/yyyy HH:mm:ss" );
                    }
                    else if ( result.getDate( "ReceivingDate") != null )
                    {
                        dateIn = StringUtils.formatDateToString( result.getDate( "ReceivingDate"), "MM/dd/yyyy HH:mm:ss" );
                    }
                    //MT EQC	MT2	MT2 EQC	MT3	MT3 EQC

                    if( result.getInt("BANK") > 0 || result.getInt("IQC") > 0 || result.getInt("PM") > 0 || result.getInt("F/T") > 0 || result.getInt("EQC") > 0
                     || result.getInt("WAIT") > 0 || result.getInt("L(B)/S") > 0 || result.getInt("B/I") > 0 || result.getInt("VB") > 0
                     || result.getInt("T/R") > 0 || result.getInt("V/M") > 0 || result.getInt("FQC") > 0 || result.getInt("BAKE") > 0
                     || result.getInt("PACK") > 0|| result.getInt("OQC") > 0 || result.getInt("DS") > 0 || result.getInt("MT") > 0 || result.getInt("MT EQC") > 0
                     || result.getInt("MT2") > 0|| result.getInt("MT2 EQC") > 0 || result.getInt("MT3") > 0 || result.getInt("MT3 EQC") > 0 || result.getInt("INV") > 0 )
                    {
                        log("In FT" + vendorLotNumber );
                        testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                        if (testLot == null)
                        {
                            testLot = new TestLot( VENDOR_NAME_SIGURD);
                            m_testDataMap.put( vendorLotNumber + fabLotNumber + partNumber, testLot);
                        }
                        if (result.getInt("BANK") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt(testLot.getQtyIn()) + result.getInt("BANK") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("BANK")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("BANK")+"");
                            }

                            if (testLot.getStage() != null && !"BANK".equalsIgnoreCase(testLot.getStage() ) )
                            {
                                testLot.setStage(testLot.getStage() + ", BANK");
                            }
                            else
                            {
                                testLot.setStage("BANK");
                            }
                        }
                        if (result.getInt("IQC") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt(testLot.getQtyIn()) + result.getInt("IQC") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("IQC")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("IQC")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", IQC");
                            }
                            else
                            {
                                testLot.setStage("IQC");
                            }
                        }
                        if (result.getInt("PM") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("PM") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("PM")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("PM")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", PM");
                            }
                            else
                            {
                                testLot.setStage("PM");
                            }
                        }
                        if (result.getInt("F/T") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt(testLot.getQtyIn()) + result.getInt("F/T") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("F/T")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("F/T")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", F/T");
                            }
                            else
                            {
                                testLot.setStage("F/T");
                            }
                        }
                        if (result.getInt("EQC") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("EQC") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("EQC")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("EQC")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", EQC");
                            }
                            else
                            {
                                testLot.setStage("EQC");
                            }
                        }
                        if (result.getInt("WAIT") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt(testLot.getQtyIn()) + result.getInt("WAIT") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("WAIT")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("WAIT")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", WAIT");
                            }
                            else
                            {
                                testLot.setStage("WAIT");
                            }
                        }
                        if (result.getInt("L(B)/S") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt(testLot.getQtyIn()) + result.getInt("L(B)/S") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("L(B)/S")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("L(B)/S")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", L(B)/S");
                            }
                            else
                            {
                                testLot.setStage("L(B)/S");
                            }
                        }
                        if (result.getInt("B/I") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("B/I") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("B/I")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("B/I")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", B/I");
                            }
                            else
                            {
                                testLot.setStage("B/I");
                            }
                        }
                        if (result.getInt("VB") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("VB") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("VB")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("VB")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", VB");
                            }
                            else
                            {
                                testLot.setStage("VB");
                            }
                        }
                        if (result.getInt("T/R") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("T/R") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("T/R")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("T/R")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", T/R");
                            }
                            else
                            {
                                testLot.setStage("T/R");
                            }
                        }
                        if (result.getInt("V/M") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("V/M") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("V/M")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("V/M")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", V/M");
                            }
                            else
                            {
                                testLot.setStage("V/M");
                            }
                        }
                        if (result.getInt("FQC") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("FQC") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("FQC")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("FQC")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", FQC");
                            }
                            else
                            {
                                testLot.setStage("FQC");
                            }
                        }
                        if (result.getInt("BAKE") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("BAKE") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("BAKE")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("BAKE")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", BAKE");
                            }
                            else
                            {
                                testLot.setStage("BAKE");
                            }
                        }
                        if (result.getInt("PACK") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( Integer.parseInt(testLot.getQtyIn()) + result.getInt("PACK") +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("PACK")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("PACK")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", PACK");
                            }
                            else
                            {
                                testLot.setStage("PACK");
                            }
                        }
                        if (result.getInt("OQC") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("OQC") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("OQC")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("OQC")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", OQC");
                            }
                            else
                            {
                                testLot.setStage("OQC");
                            }
                        }
                        if (result.getInt("DS") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("DS") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("DS")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("DS")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", DS");
                            }
                            else
                            {
                                testLot.setStage("DS");
                            }
                        }
                        if (result.getInt("MT") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("MT") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("MT")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("MT")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", MT");
                            }
                            else
                            {
                                testLot.setStage("MT");
                            }
                        }
                        if (result.getInt("MT EQC") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("MT EQC") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("MT EQC")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("MT EQC")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", MT EQC");
                            }
                            else
                            {
                                testLot.setStage("MT EQC");
                            }
                        }
                        if (result.getInt("MT2") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("MT2") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("MT2")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("MT2")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", MT2");
                            }
                            else
                            {
                                testLot.setStage("MT2");
                            }
                        }
                        if (result.getInt("MT2 EQC") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("MT2 EQC") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("MT2 EQC")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("MT2 EQC")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", MT2 EQC");
                            }
                            else
                            {
                                testLot.setStage("MT2 EQC");
                            }
                        }
                        if (result.getInt("MT3") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("MT3") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("MT3")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("MT3")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", MT3");
                            }
                            else
                            {
                                testLot.setStage("MT3");
                            }
                        }
                        if (result.getInt("MT3 EQC") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("MT3 EQC") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("MT3 EQC")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("MT3 EQC")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", MT3 EQC");
                            }
                            else
                            {
                                testLot.setStage("MT3 EQC");
                            }
                        }
                        if (result.getInt("INV") > 0 )
                        {
                            testLot = m_testDataMap.get( vendorLotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("INV") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("INV")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("INV")+"");
                            }

                            if (testLot.getStage() != null)
                            {
                                testLot.setStage(testLot.getStage() + ", INV");
                            }
                            else
                            {
                                testLot.setStage("INV");
                            }
                        }
                        if ( testLot.getQtyIn() != null && !"0".equalsIgnoreCase( testLot.getQtyIn() ) )
                        {
                            testLot.setLotNumber( vendorLotNumber );
                            testLot.setPartNumber( partNumber );
                            if ( "HOLD".equalsIgnoreCase(result.getString("Status") ) || "WAIT".equalsIgnoreCase(result.getString("Status") ) )
                            {
                                testLot.setStatus( LOT_STATUS_HOLD );
                            }
                            else
                            {
                                testLot.setStatus( LOT_STATUS_RUNNING );
                            }
                            testLot.setFabLotNumber( fabLotNumber );
                            testLot.setPsType( LOCATION_TYPE_FT );
                            testLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            testLot.setDateIn( dateIn );
                            testLot.setPoNumber( result.getString("CustomerFormNo"));
                            if ( result.getDate("RSD") != null )
                            {
                                testLot.setExpecetedDate( StringUtils.formatDateToString(result.getDate("RSD"), "MM/dd/yyyy HH:mm:ss"));
                            }
                        }
                    }
                }
            }
        }
        catch(Exception e )
        {
            e.printStackTrace();
            throw e;
        }
    }

    private void log( String message )
    {
      if ( message == null ) return;

      if ( m_parserLogger != null )
      {
          m_parserLogger.logDebug( message + LINE_SEPARATOR );
      }
      else
      {
          System.out.println( message );
      }
    } // end of log method

}

